<?php

namespace Application\model;

use Cola\Model;

class NoticeModel extends Model
{

    /**
     * 获取大区ID
     * @param $cpid
     * @return array
     */
    public function getSrids($cpid)
    {
        $srids = [];
        $ChannelModel = $this->model('Channel');
        // 获取渠道数据
        $channel = $ChannelModel->getChannelInfo();
        // 获取包数据
        $channelPackage = $ChannelModel->getChannelPackageInfoByCpid($cpid);
        // 是否有包的大区id
        if ($channelPackage && $channelPackage['srid']) {
            $srid = $channelPackage['srid']; // 这里的格式有可能是 3,10;4,11/3,10/3
            // 检测是否有逗号，无则表示只选中一个大区，保留不做处理
            if (strpos($srid, ",") !== false) {
                // 首先根据分号分割
                $g = explode(';', $srid);
                foreach ($g as $item) {
                    list($sr, $s) = explode(',', $item);
                    $srids[] = $sr;
                }
            } else {
                $srids[] = $srid;
            }
        } else {
            // 是否有渠道的大区id
            if ($channel){
                $srids = explode(',', $channel['srid']);
            }
        }

        return $srids;
    }

    /**
     * 获取公告内容
     * @param $cpid
     * @param $srid
     * @return array|bool|mixed|\PDOStatement|null
     * @throws \Cola\Exception\DbException
     */
    public function getLoginNotice($cpid, $srid)
    {
        $dt = date('Y-m-d H:i:s');
        //$cpidWhere = "`cpid`= '$cpid'";
        $cpidWhere = "FIND_IN_SET('{$cpid}',cpid)";
        $sql = "select tag,title,content,sort,update_dt as updateDt from gm_notice_login where {$cpidWhere} and srid in ($srid) and expires_dt > '$dt' order by sort desc";
        $notices = $this->db->sql($sql);
        if ($notices) {
            foreach ($notices as &$notice) {
                $notice['content'] = htmlToBBCode($notice['content']);
            }
        }
        return $notices;
    }

}